package models.application;

import java.util.*;

import javax.persistence.*;

import com.avaje.ebean.Ebean;
import models.pubobj.AniObjectState;

import play.db.ebean.*;

@Entity
public class AniApiParam extends Model {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7963088904812268538L;

	@Id
	public Long id;
	
	@ManyToOne
	public AniApi hostApi;
	
	public String paramName;
	
	public String paramValue;
	
	public Boolean isRequired;
	
	public String description;

    @ManyToOne
    public AniObjectState state;

    public AniApiParam(){};

    public AniApiParam(String paramName, String paramValue, Boolean required, String description) {
        this.paramName = paramName;
        this.paramValue = paramValue;
        isRequired = required;
        this.description = description;
        this.state = AniObjectState.findByStateKey(AniObjectState.States.ACTIVATED.name());
    }

    public AniApiParam(Long id, AniApi hostApi, String paramName, String paramValue, Boolean required, String description) {
        this.id = id;
        this.hostApi = hostApi;
        this.paramName = paramName;
        this.paramValue = paramValue;
        isRequired = required;
        this.description = description;
        this.state = AniObjectState.findByStateKey(AniObjectState.States.ACTIVATED.name());
    }

    public AniApiParam(AniApi hostApi, String paramName, String paramValue, Boolean required, String description) {
        this.hostApi = hostApi;
        this.paramName = paramName;
        this.paramValue = paramValue;
        isRequired = required;
        this.description = description;
        this.state = AniObjectState.findByStateKey(AniObjectState.States.ACTIVATED.name());
    }

    public static Finder<Long, AniApiParam> find = new Finder<Long, AniApiParam>(Long.class, AniApiParam.class);
	
	public static List<AniApiParam> findAll() {
        return find.all();
    }

	public static HashMap<String, String> paramsToMap(List<AniApiParam> paramList){
		HashMap<String, String> params = new HashMap<String, String>();
		for(AniApiParam oneParam: paramList){
			params.put(oneParam.paramName, oneParam.paramValue);
		}
		return params;
	}
    public static Integer getApiParamNumWithNames(AniApi hostApi, List<String> paramsName){
        System.out.println("PARAMS NAME LENGTH = " + paramsName.size());
        Integer apiParamNum = AniApiParam.find.where()
                .eq("hostApi.id", hostApi.id)
                .eq("state.id", AniObjectState.findByStateKey(AniObjectState.States.ACTIVATED.name()).id)
                .in("paramName", paramsName)
                .findRowCount();
        System.out.println("PARAM NUM = " + apiParamNum);
        return apiParamNum;
    }
    public static Integer getApiParamNumWithIds(List<Long> apiParamIds, AniApi hostApi){
        return AniApiParam.find.where()
                .idIn(apiParamIds)
                .eq("state.id", AniObjectState.findByStateKey(AniObjectState.States.ACTIVATED.name()).id)
                .findRowCount();
    }
    public static void deleteExcludedApiParamsWithName(List<String> apiParamsName, AniApi oneApi){
        List<AniApiParam> excludeParams = find.where()
                .eq("hostApi.id", oneApi.id)
                .not(
                        find.getExpressionFactory().in("paramName", apiParamsName)
                )
                .findList();
        for(AniApiParam oneExcludeParam: excludeParams){
            oneExcludeParam.state = AniObjectState.findByStateKey(AniObjectState.States.RETIRED.name());
        }
        Ebean.save(excludeParams);
    }
}
